﻿using FlSigCaptLib;
using FLSIGCTLLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SignatureBoard
{
    class WacomSignatureBoard
    {
        /// <summary>
        /// 签名
        /// </summary>
        /// <param name="licens">授权字符串</param>
        /// <param name="tipsContent">签名时，在签名版上显示的提示内容</param>
        /// <param name="tipsTitle">签名时，在签名版上显示的提示内容的标题</param>
        /// <param name="fileName">签名文件保存的路径</param>
        public bool DoSignature(string licence, string tipsContent, string tipsTitle, string fileName, out string errMsg)
        {
            try
            {
                errMsg = "";
                SigCtl sigCtl = new SigCtl();
                sigCtl.Licence = licence; //"AgAZAPZTkH0EBVdhY29tClNESyBTYW1wbGUBAoECA2UA";
                //sigCtl.BorderVisible = false;

                DynamicCapture dc = new DynamicCapture();
                DynamicCaptureResult res = dc.Capture(sigCtl, tipsContent, tipsTitle, null, null);
                if (res == DynamicCaptureResult.DynCaptOK)
                {
                    SigObj sigObj = (SigObj)sigCtl.Signature;
                    //sigObj.set_ExtraData("AdditionalData", "C# test: Additional data");

                    sigObj.RenderBitmap(fileName, -200, -200, "image/bmp", 0.7f, 0x000000, 0xffffff, -1.0f, -1.0f,
                        RBFlags.RenderOutputFilename | RBFlags.RenderColor32BPP | RBFlags.RenderEncodeData);
                    //sigObj.RenderBitmap(m_strSignPath, -150, -150, "image/bmp", 0.7f, 0x000000, 0xffffff, -1.0f, -1.0f, RBFlags.RenderOutputFilename | RBFlags.RenderColor32BPP | RBFlags.RenderEncodeData);
                }
                else
                {
                    switch (res)
                    {
                        case DynamicCaptureResult.DynCaptCancel:
                            errMsg = "未捕获签名，因为用户取消了签名捕获窗口。";
                            break;
                        case DynamicCaptureResult.DynCaptError:
                            errMsg = "系统错误或其他一些异常错误情况。";
                            //MessageBox.Show("no capture service available");
                            break;
                        case DynamicCaptureResult.DynCaptPadError:
                            errMsg = "没有可用的捕获服务；通常，找不到可用于捕获签名的签名板。";
                            //MessageBox.Show("signing device error");
                            break;
                        case DynamicCaptureResult.DynCaptIntegrityKeyInvalid:
                            errMsg = "完整性密钥参数无效（已过时）";
                            break;
                        case DynamicCaptureResult.DynCaptNotLicensed:
                            errMsg = "组件尚未获得执行捕获的许可。这可能是因为尚未设置适当的许可证，或者由于未满足许可证的条件，例如未找到所需的硬件，例如特定的签名板类型。";
                            break;
                        case DynamicCaptureResult.DynCaptAbort:
                            errMsg = "未捕获签名，因为Precapture事件的处理程序返回了false。使用sictlxhtml，这可能表示无法分析文档内容。";
                            break;
                        default:
                            errMsg = "未知错误:" + res.ToString();
                            break;
                    }

                    return false;
                }

                return true;
            }
            catch (System.Exception e)
            {
                throw e;
            }

        }
    }
}
